package com.ls.lserp.modules.setting.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.record.UseSelFSRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.modules.setting.dao.QueryCompanyDao;
import com.ls.lserp.modules.setting.dao.WarehouseShelvesDao;
import com.ls.lserp.modules.setting.entity.CompanyList;
import com.ls.lserp.modules.setting.entity.Duty;
import com.ls.lserp.modules.setting.entity.WarehouseShelves;
import com.ls.lserp.modules.sys.entity.Dict;
import com.ls.lserp.modules.sys.entity.Sequence;
import com.ls.lserp.modules.sys.utils.DictUtils;
import com.ls.lserp.modules.sys.utils.SequenceUtil;
import com.ls.lserp.modules.sys.utils.UserUtils;
@Service
@Transactional(readOnly = false)
public class WarehouseShelvesService extends CrudService<WarehouseShelvesDao,WarehouseShelves>{

	
	 @Autowired
	    private WarehouseShelvesDao warehouseShelvesDao ;

	    /**
		 * 查询父项
		 * @param page
		 * @param user
		 * @return
		 */
	    public Page<WarehouseShelves> findWarehouseList(Page<WarehouseShelves> page, WarehouseShelves warehouseShelves) {
	        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
	        //designOption.getSqlMap().put("dsf", dataScopeFilter(user.getCurrentUser(), "o", "a"));
	        // 设置分页参数
	    	warehouseShelves.setPage(page);
	        // 执行分页查询
	        page.setList(warehouseShelvesDao.findList(warehouseShelves));
	        return page;
	    }
	    
	    /**
		 * 查询一条数据
		 * 
		 * @param user
		 * @return
		 */
	    public WarehouseShelves findAWarehouse(WarehouseShelves warehouseShelves){
	    	warehouseShelves = warehouseShelvesDao.get(warehouseShelves);
	    	List<WarehouseShelves> shelvesList = warehouseShelvesDao.findShelvesList(warehouseShelves);
	    	warehouseShelves.setWarehouseShelvesList(shelvesList);
	    	return warehouseShelves;
	    }
	    
	    /**
		 * ID查询一条数据
		 * 
		 * @param user
		 * @return
		 */
	    public WarehouseShelves findByID(Long id){
	    	
	    	return warehouseShelvesDao.get(id);
	    }
	    /**
		 * 查询生成编码相关数据
		 * 
		 * @param user
		 * @return
		 */
	    public WarehouseShelves userMessege(WarehouseShelves warehouseShelves){
	    	
	    	return warehouseShelvesDao.userMessege(warehouseShelves);
	    }
	    
	    /**
		 * 生成仓库代码
		 * @param user
		 */
	    public WarehouseShelves getWarehouseCode(WarehouseShelves warehouseShelves) {
	    	warehouseShelves.setCreateBy(UserUtils.getUser());
	    	WarehouseShelves userMessege =  warehouseShelvesDao.userMessege(warehouseShelves);
	    	//公司国别
	    	Dict dictCountry=DictUtils.getDictEntity(userMessege.getCompanyCountry());
	    	//公司地区
	    	//Dict dictCity=DictUtils.getDictEntity(userMessege.getCompanyCity());
	    	//公司简称
	    	String companyShortName=userMessege.getCompanyShortName().toUpperCase();
		    			//若为null 执行插入语句（新建）
		    				//楼层位置
		    		        String place=warehouseShelves.getWarehousePlace();
		    		        
		    		        StringBuilder conCode = new StringBuilder();
		    		        conCode.append(dictCountry.getItemShortNameEn());
		    		        //conCode.append(dictCity.getItemShortNameEn());
		    		        conCode.append(companyShortName);
		    		        conCode.append(place);
		    		        //conCode.append("000000");
		    		        // 仓库编号
		    		        Sequence sequence=SequenceUtil.ognizationCode(conCode.toString(), "sequence_warehouse", 6);
		    		        warehouseShelves.setWarehouseCode(conCode.toString().toUpperCase());
		    		        warehouseShelves.setWarehouseBarCode(SequenceUtil.warehousebarcode().getReturnCode());
		    		        return warehouseShelves;
	    }
	    
	    /**
		 * 新增/修改一条数据
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void saveWarehouse(WarehouseShelves warehouseShelves) {
	    	warehouseShelves.setCreateBy(UserUtils.getUser());
	    	WarehouseShelves userMessege =  warehouseShelvesDao.userMessege(warehouseShelves);
	    	//公司国别
	    	Dict dictCountry=DictUtils.getDictEntity(userMessege.getCompanyCountry());
	    	//公司地区
	    	//Dict dictCity=DictUtils.getDictEntity(userMessege.getCompanyCity());
	    	//公司简称
	    	String companyShortName=userMessege.getCompanyShortName().toUpperCase();
	    	// 判断新建编辑
	    			//若为null 执行插入语句（新建）
	    			if (warehouseShelves.getId()==null){
	    				//楼层位置
	    		        String place=warehouseShelves.getWarehousePlace();
	    		        
	    		        StringBuilder conCode = new StringBuilder();
	    		        conCode.append(dictCountry.getItemShortNameEn());
	    		        //conCode.append(dictCity.getItemShortNameEn());
	    		        conCode.append(companyShortName);
	    		        conCode.append(place);
	    		        //conCode.append("000000");
	    		        // 仓库编号
	    		        Sequence sequence=SequenceUtil.ognizationCode(conCode.toString(), "sequence_warehouse", 6);
	    		        warehouseShelves.setWarehouseCode(conCode.toString().toUpperCase());
	    		        warehouseShelves.setWarehouseBarCode(SequenceUtil.warehousebarcode().getReturnCode());
	    				
	    				warehouseShelvesDao.insert(warehouseShelves);
	    		    	if (warehouseShelves.getWarehouseShelvesList()!=null) {
							
	    		    		warehouseShelvesDao.shelvesInsert(warehouseShelves);
						}
	    		    	
	    				
	    			// 编辑
	    			}else{
	    				// 更新数据
	    				warehouseShelvesDao.update(warehouseShelves);
	    				//warehouseShelvesDao.deleteShelves(warehouseShelves);
	    				List<WarehouseShelves> shelvesList = warehouseShelves.getWarehouseShelvesList();
	    				if (shelvesList!=null&&shelvesList.size()!=0) {
		    				for (int i = 0; i < shelvesList.size(); i++) {
		    					if (shelvesList.get(i)!=null) {
		    						shelvesList.get(i).preUpdate();
		    						shelvesList.get(i).setId(warehouseShelves.getId());
		    						if (shelvesList.get(i).getShelvesId()!=null) {
		    							warehouseShelvesDao.updateShelves(shelvesList.get(i));
		    						}else {
		    							warehouseShelvesDao.insertShelves(shelvesList.get(i));
		    						}
								}
							}
	    				}
	    		}
	    }
	    
	    /**
	   	 * 删除一条数据
	   	 * 
	   	 * @param user
	   	 */
	       public void deleteWarehouse(WarehouseShelves warehouseShelves){
	       	
	    	   warehouseShelvesDao.delete(warehouseShelves);
	       	
	       	
	       }
	       
	      
	       
	       
	
}
